
<div>
	<div style="width: 48%; float: left;"
		id="productsdiv">
		<h3><span id="inv-tab">Products</span>>> Manage Products</h3>
		<hr>
		<div id="objects">&nbsp;</div>
	</div>
	<div style="width: 48%; float: right; border: 1px solid;"
		id="attributesdiv">
		<h3>Manage Attributes</h3>
		<hr>
		<div id="attributes">&nbsp;</div>

	</div>
	<div style="clear: both">&nbsp;</div>
</div>



<script type="text/javascript">

//Objects

function loadObjects(){
	   $.getJSON('<?php print base_url(); ?>xmlrpc_client/get_all_products', function(data) {
		   var items = [];
	       var text='<div class="tobjects">';

		      var i=1;
		   $.each(data, function(key, val) {
		      text=text+'<ul objid="'+val.id+'" class="table objdiv ui-widget ui-helper-clearfix">'
		      +'<li class="ui-state-default ui-corner-all fieldobj"><span class="ui-icon ui-icon-folder-open"></span></li>'   //<button objid="'+val.id+'" class="field_btn">Fields</button>
		      +'<li class="namebox"> '+val.name+'</li></ul>';

		      i++;
		   });
		   text=text+'</div>';

		   $('#objects').html(text);
		   $('#obj_sub').show();
		   $('#obj_sub_update').hide();
		   $('#obj_in').val('');
		   associateObjectActions();

	   });
}

function associateObjectActions(){
	   $(".fieldobj").each(function(){
			$(this).click(function(){
				inobj=$(this);
				ulobj=inobj.parent();
				$('ul').removeClass('tableactive');
				ulobj.addClass('tableactive');
				objid=ulobj.attr('objid');
				nameobj=ulobj.children(".namebox");
				loadattributes(objid);
			});
	   });
}


//Fields
function loadattributes(objId){

	var dataString='&objid='+objId;
	$.ajax({
		type: "POST",
			url: "<?php print base_url(); ?>xmlrpc_client/get_all_attributes",
			data: dataString,
			dataType: 'json',
			success : function(data){
				var text='';
				console.log(data.length);
				if(data.length > 0){
					text=text+'<div id="fieldsdiv">';
						text=text+'<ul style="background-color:#AAA" class="table fielddiv ui-widget ui-helper-clearfix" >'
						+'<li style="width:50px" >Active</li>'
						+'<li style="width:200px" class="fieldnamecell"><b>Datatype</b></li>'
					    +'<li class="" >&nbsp;</span></li>'
					    +'<li class="" >&nbsp;</span></li></ul>';
						$.each(data,function(key,val){
						text=text+'<ul class="fielddiv table ui-widget ui-helper-clearfix" objid="'+objId+'" prod_attr_id="'+val.id+'" ">'
						+'<li style="width:50px" >';

						if(val.activeind==1){
							text=text+'<input type="checkbox" checked="checked"/>';
						}else if(val.activeind==0){
							text=text+'<input type="checkbox"/>';
						}else{
							text=text+'&nbsp;';
						}

						text=text+'</li>'
						+'<li style="width:350px" class="fieldnamecell">'+val.attribute_name+'</li>'
						+'<li style="width:150px" class="defaultvaluecell">'+val.defaultvalue+'</li>'
					    +'<li class="ui-state-default ui-corner-all editfield" attrid='+val.attrid+' ><span class="ui-icon ui-icon-pencil"></span></li>'
					    +'<li class="ui-state-default ui-corner-all delfield" attrid='+val.attrid+' ><span class="ui-icon ui-icon-trash"></span></li></ul>';
						
					});
					 text=text+'</div></br>';
					 text=text+'<table><tr><th>Field Name</th><th>Default Value</th></tr>';
					 text=text+''
					 +'<td><input type="text" id="in_fieldname" name="in_fieldname"/></td>'
					 +'<td><input type="text" id="in_defaultvalue" name="in_defaultvalue"/></td>'
					 +'<td><div id="field_btn_cont"><button objid="'+objId+'" id="update_field_btn">Update</button><button id="add_field_btn"  objid="'+objId+'" ">Add</button></div></td></tr>';
					text=text+'</table>';
				
					
				}
				$('#attributesdiv').html(text);
				associateFieldActions();
				
			}
	});
}

function associateFieldActions(){


	   $(".editfield").each(function(){

		   $(this).click(function(){

				inobj=$(this);
				ulobj=inobj.parent();

				attrid=inobj.attr('attrid');

			   var fieldName=ulobj.children('.fieldnamecell').text();
			   var defaultvalue=ulobj.children('.defaultvaluecell').text();

			   $('#in_fieldname').val(fieldName);
			   $('#in_defaultvalue').val(defaultvalue);

			  $('#update_field_btn').click(function(){
				  var objid=$(this).attr('objid');
					var dataString='&prod_attr_id='+ulobj.attr('prod_attr_id')+
					'&fieldname='+$('#in_fieldname').val()+
					'&defaultvalue='+$('#in_defaultvalue').val()+'&objid='+objid
					+'&attrid='+attrid;

					$.ajax({
						type: "POST",
							url: "<?php print base_url(); ?>xmlrpc_client/update_attribute",
							data: dataString,
							success : function(data){
								   $('#in_fieldname').val("");
								   $('#in_defaultvalue').val("");
								   loadattributes(objid);
								$.jGrowl("Attribute is updated.!");
							}
					});
					console.log(fieldName);
			  });
			   	
		   });
	   });

	   $(".delfield").each(function(){

		   $(this).click(function(){
				inobj=$(this);
				ulobj=inobj.parent();
				attrid=inobj.attr('attrid');
				var dataString='&attrid='+attrid;
				var objid=ulobj.attr('objid');	
				
				$.ajax({
					type: "POST",
						url: "<?php print base_url(); ?>xmlrpc_client/delete_attribute",
						data: dataString,
						success : function(data){
							loadattributes(objid);
							$.jGrowl("Attribute is deleted.!");
						}
				});
		   });
	   });


	   $('#add_field_btn').click(function(){

		    var objid=$(this).attr('objid');
			var dataString='&fieldname='+$('#in_fieldname').val()+
			'&defaultvalue='+$('#in_defaultvalue').val()+'&objid='+objid;

			$.ajax({
				type: "POST",
					url: "<?php print base_url(); ?>xmlrpc_client/create_attribute",
					data: dataString,
					success : function(data){
						   $('#in_fieldname').val("");
						   $('#in_defaultvalue').val("");
						   loadattributes(objid);
							$.jGrowl("Attribute is created.!");
					}
			});x
	   });
}


$(document).ready(function(){
	loadObjects();
	$('#inv-tab').click(function(){
		$('#tab-container').load("<?php print base_url(); ?>admin/loadtab/inventory");
	});
});
</script>
